
// <copyright file="GenericDbDataSink.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   This class is the generic implementation of a IDbDataSink{T}.
//   It uses an IDbCallbackStrategy and this strategy has to be passed to its constructor.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System.Collections.Generic;

    using SubhadraSolutions.Sharp.Utils.Data.Helpers;

    /// <summary>
    /// This class is the generic implementation of a IDbDataSink{T}.
    ///     It uses an IDbCallbackStrategy and this strategy has to be passed to its constructor.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class GenericDbDataSink<T> : IDbDataSink<T>
    {
        #region Fields

        /// <summary>
        ///     The _callback strategy.
        /// </summary>
        private readonly IDbCallbackStrategy<T> _callbackStrategy;

        /// <summary>
        /// The context.
        /// </summary>
        private DbTransactionContext _context;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericDbDataSink{T}"/> class.
        /// </summary>
        /// <param name="callbackStrategy">
        /// The callback strategy.
        /// </param>
        public GenericDbDataSink(IDbCallbackStrategy<T> callbackStrategy)
        {
            this._callbackStrategy = callbackStrategy;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// Inserts or updates the given Dtosinto the database.
        /// </summary>
        /// <param name="dataTransferObjects">
        /// The Dtosthat have to be inserted or updated in the database.
        /// </param>
        /// <param name="tag">
        /// The tag.
        /// </param>
        public virtual void InsertOrUpdate(IEnumerable<T> dataTransferObjects, object tag = null)
        {
            DatabaseHelper.Execute(dataTransferObjects, this._callbackStrategy, this._context, tag);
        }

        /// <summary>
        /// Sets the database transaction context.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        public void SetDbTransactionContext(DbTransactionContext context)
        {
            this._context = context;
        }

        #endregion Public Methods and Operators
    }
}